/*
 * Created on Jul 20, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.herohead;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
//import javax.mail.MessagingException;
import javax.mail.Multipart;
//import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

/**
 * Email processes
 * 
 * @author Craig David - Columbus OHIO, USA
 */
public class Email {

	String host;
	Properties props = new Properties();
	Session session = Session.getInstance(props,null);
	MimeMessage message = new MimeMessage(session);

	int recipientCnt = 0;


	/**
	 * 
	 * @param host
	 */
	public Email (String host) {
		this.host = host;
	}

	/**
	 * add BCC email address to message (internet email address format)
	 * @param BCCAddress
	 */

	public void addBCCAddress (String BCCAddress) {

		String[] tokens = BCCAddress.split(",");
		addBCCAddress(tokens);
	}

	/**
	 * Add bcc email
	 * 
	 * @param bccAddress array of Strings
	 */
	public void addBCCAddress (String[] bccAddress) {

		for (int i = 0; i < bccAddress.length; i++) {
			try {
				InternetAddress bcc = new InternetAddress(bccAddress[i]);
				message.addRecipient(Message.RecipientType.BCC, bcc);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			recipientCnt++;
		}
	}
	/**
	 * add CC email address to message (internet email address format)
	 * 
	 * @param CCAddress
	 */
	public void addCCAddress (String CCAddress) {

		String[] tokens = CCAddress.split(",");
		addCCAddress(tokens);
	}

	public void addCCAddress (String[] ccAddress) {

		for (int i = 0; i < ccAddress.length; i++) {
			try {
				InternetAddress cc = new InternetAddress(ccAddress[i]);
				message.addRecipient(Message.RecipientType.CC, cc);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			recipientCnt++;
		}
	}


	/**
	 * Add from email address (internet email address format)
	 * @param fromAddress
	 */

	public void addFromAddress (String fromAddress) {

		try {
			InternetAddress from = new InternetAddress(fromAddress);
			message.setFrom(from);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * add To email address
	 * 
	 * @param toAddress (internet email address format)
	 */
	public void addToAddress (String toAddress) {

		String[] tokens = toAddress.split(",");
		addToAddress(tokens);
	}
	/**
	 * add To address
	 * @param toAddress Array of Strings
	 */
	public void addToAddress (String[] toAddress) {

		for (int i = 0; i < toAddress.length; i++) {
			try {
				InternetAddress to = new InternetAddress(toAddress[i]);
				message.addRecipient(Message.RecipientType.TO, to);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			recipientCnt++;
		}
	}
	/**
	 * Check if any check recipients are set.
	 *
	 */
	private void checkRecipients() {
		if (recipientCnt == 0) {
			System.out.println("No recipients to send email to!!!!!");
			System.exit(1);
		}
	}

	/**
	 * Return Host
	 * @return host address
	 */
	public String getHost() {
		return host;
	}
	/**
	 * Sends HTML Mail.
	 * @param subject
	 * @param theMessage
	 */
	public void sendHTMLMail(String subject, String theMessage) {

		checkRecipients();

		try {
			props.put("mail.smtp.host", host);
			message.setSubject(subject);
			Multipart multipart = new MimeMultipart("alternative");
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(theMessage);

			multipart.addBodyPart(messageBodyPart);
			messageBodyPart = new MimeBodyPart();
			String htmlText = theMessage;
			messageBodyPart.setContent(htmlText, "text/html");

			multipart.addBodyPart(messageBodyPart);

			message.setContent(multipart);

			Transport.send(message);

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void sendHTMLMailWithText(String subject, String theMessage, String theTxtMessage) {

		checkRecipients();

		try {
			props.put("mail.smtp.host", host);
			message.setSubject(subject);
			Multipart multipart = new MimeMultipart("alternative");
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(theTxtMessage);

			multipart.addBodyPart(messageBodyPart);
			messageBodyPart = new MimeBodyPart();
			String htmlText = theMessage;
			messageBodyPart.setContent(htmlText, "text/html");

			multipart.addBodyPart(messageBodyPart);

			message.setContent(multipart);

			Transport.send(message);

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * Send Plain text email

	 * @param subject String subject
	 * @param theMessage String message to include in the body
	 */

	//	############################################################################
	public void sendMail(String subject, String theMessage){
		//	############################################################################

		checkRecipients();
		props.put("mail.smtp.host", host);

		try {
			message.setSubject(subject);
			message.setText(theMessage);
			Transport.send(message);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * send Email with an Attachment and also with text.
	 * this version can only attach a single file
	 * 
	 * @param subject String subject
	 * @param theMessage String message to include in the body
	 * @param fle filename to attach
	 */
	//	############################################################################
	public void sendMailAtt(String subject, String theMessage, String fle){
		//	############################################################################

		checkRecipients();

		props.put("mail.smtp.host", host);

		try {
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(theMessage);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			messageBodyPart = new MimeBodyPart();
			DataSource source = new FileDataSource(fle);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(fle);
			multipart.addBodyPart(messageBodyPart);

			message.setContent(multipart);

			Transport.send(message);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * send Email with an Attachment and also with text.
	 * this version can only attach a single file
	 * 
	 * @param subject String subject
	 * @param theMessage String message to include in the body
	 * @param fle filename to attach
	 */
	//	############################################################################
	public void sendMailAttv2(String subject, String theMessage, String fle){
		//	############################################################################

		checkRecipients();

		props.put("mail.smtp.host", host);

		try {
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(theMessage);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			messageBodyPart = new MimeBodyPart();
			//DataSource source = new FileDataSource(fle);
			byte[] bytes;
			bytes = fle.getBytes();
			DataSource dataSource = new ByteArrayDataSource(bytes, "application/txt");
			MimeBodyPart attBodyPart = new MimeBodyPart();
			attBodyPart.setDataHandler(new DataHandler(dataSource));
			attBodyPart.setFileName("test.xls");

			multipart.addBodyPart(attBodyPart);

			message.setContent(multipart);

			Transport.send(message);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void sendMailNow(String uname) throws Throwable  {

		String host = "smtp.gmail.com";
		String username = "herohead@gmail.com";
		String password = "kd8phikd8phi";
		Properties props = new Properties();
		props.put("mail.smtps.auth", "true");

		//String msg = "this is a test message";

		MimeMessage msg = new MimeMessage(session);
		msg.setSubject("test from java");
		msg.setText("body of message");
		Transport t = session.getTransport("smtps");
		try {
			t.connect(host, username, password);
			t.sendMessage(msg, msg.getAllRecipients());
		} finally {
			t.close();
		}
	}


	/**
	 * To set the HOST
	 * @param toHost
	 */

	public void setHost (String toHost) {
		host = toHost;
	}


}